package com.chery.media.ui

import com.chery.media.library.base.BaseViewModel
import com.chery.media.library.extend.livedata.StringLiveData
import com.chery.media.library.extend.toTime13String
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

val mediaViewModel: MediaViewModel by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    MediaViewModel()
}
class MediaViewModel: BaseViewModel() {

    // 当前时间
    val currentTime = StringLiveData()

    // 计时器
    private var codeJob: Job? = null

    // 是否继续
    var isContinue = true

    fun startGetTime() {
        codeJob = CoroutineScope(Dispatchers.Main + SupervisorJob()).launch {
            do {
                val nowTime = System.currentTimeMillis()
                currentTime.postValue(nowTime.toTime13String("HH:mm"))
                delay(1000L)
            } while (isContinue)
        }
    }

    override fun onCleared() {
        super.onCleared()
        isContinue = false
        codeJob?.cancel()
    }

}